package com.imooc.controller;


import com.imooc.pojo.Users;
import com.imooc.pojo.vo.UsersVo;
import com.imooc.service.UserService;
import com.imooc.utils.IMoocJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@Api(value = "用户注册登录注销", tags = {"用户注册登录注销"})
public class RegisterLoginController extends BaseController {
    @Autowired
    private UserService userService;

    @PostMapping("/regist")
    @ApiOperation(value = "用户注册接口", notes = "用户注册")
    public IMoocJSONResult regist(@RequestBody Users users) {
        //判断用户名和密码是否为空
        if (StringUtils.isBlank(users.getUsername()) || StringUtils.isBlank(users.getPassword())) {
            return IMoocJSONResult.errorMsg("用户名和密码不能为空");
        }
        //判断用户名是否可用
        // boolean b = userService.queryUserNameIsExist(users.getUsername());
        if (userService.queryUserNameIsExist(users.getUsername())) {
            return IMoocJSONResult.errorMsg("用户名已存在");
        } else {
            //保存用户的信息
            Users user = userService.saveUser(users);
            user.setPassword("");
            UsersVo usersVo = setUserRedisSession(user);
            return IMoocJSONResult.ok(usersVo);
        }


    }


    /**
     * 用户的登录接口
     */

    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录的接口")
    public IMoocJSONResult login(@RequestBody Users users) {
        //1.判断用户名和密码不为空
        if (StringUtils.isBlank(users.getUsername()) || StringUtils.isBlank(users.getPassword())) {
            return IMoocJSONResult.errorMsg("用户名和密码不能为空");
        }
        //2.根据传递的信息去查询数据
        Object loginResult = userService.login(users);
        if (loginResult instanceof String) {
            return IMoocJSONResult.errorMsg((String) loginResult);
        } else if (loginResult instanceof Users) {
            Users user = (Users) loginResult;
            //调用redis
            UsersVo usersVo = setUserRedisSession(user);
            return IMoocJSONResult.ok(usersVo);
        } else {
            return IMoocJSONResult.errorMsg("登录失败");
        }


    }

    /**
     * 用户的登录接口
     */

    @PostMapping("/logout")
    @ApiOperation(value = "用户注销登录", notes = "用户登录的接口")
    @ApiImplicitParam(name = "userId", value = "用户id",
            required = true, dataType = "String", paramType = "query")
    public IMoocJSONResult logout(String userId) {


        redisOperator.del(USER_REDIS_SESSION + ":" + userId);

        return IMoocJSONResult.ok("注销成功");


    }

    /**
     * 设置用户的sessiontoken
     *
     * @param user
     * @return
     */
    private UsersVo setUserRedisSession(Users user) {
        String uniqueToken = UUID.randomUUID().toString();
        setRedisForUser(USER_REDIS_SESSION + ":" + user.getId(), uniqueToken);
        UsersVo usersVo = new UsersVo();
        BeanUtils.copyProperties(user, usersVo);
        usersVo.setUserToken(uniqueToken);
        return usersVo;
    }


}
